package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.blog.constant.QrCodeStatusConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.blog.constant.Constants;
import com.ruoyi.blog.core.domain.AjaxResult;
import com.ruoyi.blog.core.domain.entity.SysMenu;
import com.ruoyi.blog.core.domain.entity.SysUser;
import com.ruoyi.blog.core.domain.model.LoginBody;
import com.ruoyi.blog.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @GetMapping("getBlogInfo")
    public AjaxResult getBlogInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user); // 用户信息
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
//        ajax.put("blogConfig", blogConfigService.getBlogConfig());
        return ajax;
    }

    @PostMapping("changeRole")
    public AjaxResult changeRole(Long roleId)
    {
        // 传入要切换的角色 id, 在登录完成之后，只会展示一个角色
        return AjaxResult.success();
    }

    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 生成二维码
     */
    @GetMapping("/qrcode")
    public AjaxResult generateQrCode() {
        String qrCodeId = loginService.getQrCode();
        return AjaxResult.success("qrCodeId", qrCodeId);
    }

    /**
     * 检查扫码状态
     */
    @GetMapping("/qrcode/status/{qrCodeId}")
    public AjaxResult checkQrCodeStatus(@PathVariable String qrCodeId) {
        int status = loginService.checkQrCodeStatus(qrCodeId);
        if (status == QrCodeStatusConstants.QR_CODE_STATUS_WAITING) {
            return AjaxResult.success("等待用户扫码").put("status", status);
        } else if (status == QrCodeStatusConstants.QR_CODE_STATUS_SCANNED) {
            return AjaxResult.success("用户已扫码，等待确认").put("status", status);
        } else if (status == QrCodeStatusConstants.QR_CODE_STATUS_CONFIRMED) {
            String token = loginService.generateTokenAfterQrCodeConfirmed(qrCodeId);
            return AjaxResult.success("token", token).put("status", status);
        } else {
            return AjaxResult.error("二维码已过期或无效");
        }
    }

    /**
     * 确认扫码登录
     */
    @PostMapping("/qrcode/confirm")
    public AjaxResult confirmQrCodeLogin(@RequestParam String qrCodeId, @RequestParam String username, @RequestParam String password) {
        boolean isConfirmed = loginService.confirmQrCodeLogin(qrCodeId, username, password);
        if (isConfirmed) {
            return AjaxResult.success("确认成功");
        } else {
            return AjaxResult.error("确认失败");
        }
    }



}
